package mysql

import (
	"doc-manage/doctorrpc/doctorrpc"
	"gorm.io/gorm"
)

type Doctor struct {
	gorm.Model
	DoctorName  string `gorm:"type:varchar(30);comment:医生姓名"`
	DoctorImage string `gorm:"type:varchar(30);comment:医生头像"`
}

func (d *Doctor) DoctorAdd(db *gorm.DB) error {
	return db.Debug().Create(d).Error
}

func (d *Doctor) DoctorUpdate(db *gorm.DB, in *doctorrpc.DoctorUpdateRequest) error {
	doctor := Doctor{
		DoctorName:  in.DoctorName,
		DoctorImage: in.DoctorImage,
	}

	return db.Debug().Where("id=?", in.DoctorId).Updates(doctor).Error
}

func (d *Doctor) DoctorShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(d).Error
}

func DoctorList(db *gorm.DB) ([]*doctorrpc.DoctorList, error) {
	var doctor []*doctorrpc.DoctorList

	err2 := db.Debug().Model(&Doctor{}).Select(
		"doctors.id as doctor_id",
		"doctor_name",
		"doctor_image",
	).Find(doctor).Error

	return doctor, err2
}
